<template>
  <div>
    <!-- 整个页面需要显示内容的区域 -->
    <div class="container">
      <!-- 顶部区域 -->
      <div class="header">
        <h1>来客餐厅运营管理平台</h1>
      </div>
      <!-- 登录表单 -->
      <div class="login-form">
        <el-menu default-active="1"
                 class="el-menu-demo"
                 mode="horizontal"
                 text-color="black" @select="handleSelect">
          <el-menu-item index="1">后台管理</el-menu-item>
          <el-menu-item index="2">前台点单</el-menu-item>
        </el-menu>
        <el-form v-if="index==1" :model="ruleForm" ref="ruleForm" label-width="0px" class="demo-ruleForm">
          <el-form-item label="" prop="username">
            <el-input v-model="ruleForm.username" placeholder="请输入用户名">
              <template slot="prepend"><i class="el-icon-user-solid"/></template>
            </el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input v-model="ruleForm.password" show-password placeholder="请输入密码" @keyup.enter.native="submitForm('ruleForm')">
              <template slot="prepend"><i class="el-icon-lock"/></template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%;"
                       @click="submitForm('ruleForm')">登录
            </el-button>
          </el-form-item>
        </el-form>
        <el-form v-if="index==2" :model="ruleForm" ref="ruleForm" label-width="0px" class="demo-ruleForm" >
          <el-form-item label="" prop="username">
            <el-input v-model="ruleForm.username" placeholder="请输入用户名">
              <template slot="prepend"><i class="el-icon-user-solid"/></template>
            </el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input v-model="ruleForm.password" show-password placeholder="请输入密码" @keyup.enter.native="submitForm('ruleForm')">
              <template slot="prepend"><i class="el-icon-lock"/></template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%;"
                       @click="submitForm('ruleForm')">登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 版权信息 -->
      <div class="copyright">
        <p>Copyright &copy;  公司名称：来客餐馆食品股份有限公司  </p>
        <p> 公司地址：四川省成都市天府街道  |  电话：028-25099000</p>
      </div>
    </div>
  </div>
</template>

<script>
import BaseUrl from "@/http/BaseUrl";

export default {
  data() {
    return {
      index:1,
      ruleForm: {
        username: '',
        password: '',
      }
    };
  },
  methods: {
    handleSelect(key, keyPath) {
      this.index = key;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = BaseUrl.PASSPORT + '/passport/login';
          console.log('url = ' + url);

          this.axios.post(url, this.ruleForm).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '登录成功！',
                duration:500,
                type: 'success'
              });
              let loginInfo = jsonResult.data;
              localStorage.setItem('loginInfo', JSON.stringify(loginInfo));
              if (this.index===1){
                this.$router.push('/admin/index');
              }else {
                this.$router.push('/index');
              }

            } else {
              this.$message.error(jsonResult.message);
            }
          });
        } else {
          return false;
        }
      });
    }
  }
}
</script>

<style>
body {
  height: 100vh;
  /*background-image: linear-gradient(to bottom left, #a0d06f, #468b22);*/
  background-image:url("../../public/images/background.jpg");
  background-size: cover;
  background-repeat: no-repeat;
}

.container {
  position: relative;
  top: 200px;
  width: 460px;
  margin: 0 auto;
}

.header {
  margin-top: 50px;
}

.header h1 {
  text-align: center;
  color: #fff;
}

.login-form {
  background: #fff;
  border-radius: 10px;
  padding: 50px 50px 20px 50px;
}

.copyright {
  margin-top: 20px;
}

.copyright p {
  text-align: center;
  font-size: 11px;
  color: #fff;
}
.el-menu {font-weight: bold; }
.el-menu-item.is-active {background: #fff !important;}
</style>
